//
//  LGReportTableController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/18.
//

#import "LGReportTableController.h"
#import "LGPatientCTReportView.h"
#import "LGPatientReportCell.h"
@interface LGReportTableController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)LGPatientCTReportView *headView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableArray *closeArray;

@end

@implementation LGReportTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"患者报告";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:self.tableView.bounds];
    [self requestRecordData];
    if ([self.type isEqualToString:@"1"]) {
        //CT报告
        self.tableView.tableHeaderView = self.headView;
        [self requestChartData];
        self.emptyView.frame = CGRectMake(0, kMaxY(self.headView.frame), Screen_W, self.tableView.bounds.size.height-self.headView.bounds.size.height);
    }
}
//请求CT统计图
-(void)requestChartData{
    [RequestUtil withGET:@"/picRecord/chart" parameters:@{@"patientId":self.patientid,@"type":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.headView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//请求记录列表
-(void)requestRecordData{
    [RequestUtil withGET:@"/picRecord/list" parameters:@{@"patientId":self.patientid,@"type":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无报告" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无报告" offset:0];
        }
    }];
}

-(void)openOrCloseData:(UIButton *)sender{
    NSInteger section = sender.tag-1000;
    if (sender.selected == NO && [self.closeArray containsObject:@(section)]) {
        [self.closeArray removeObject:@(section)];
    }else{
        [self.closeArray addObject:@(section)];
    }
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (![self.closeArray containsObject:@(section)]) {
        NSArray *tempArray = self.dataArray[section][@"data"];
        return tempArray.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *tempArray = self.dataArray[indexPath.section][@"data"];
    LGPatientReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = tempArray[indexPath.row];
    if(tempArray.count>1){
        if(indexPath.row == 0){
            cell.type = LGPositionTypeTop;
        }else if (indexPath.row == tempArray.count-1){
            cell.type = LGPositionTypeBottom;
        }else{
            cell.type = LGPositionTypeMiddle;
        }
    }else{
        cell.type = LGPositionTypeSingle;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    UILabel *dateLabel = [UILabel lableWithText:LGNSString(self.dataArray[section][@"date"]) colorString:@"#2B2A37" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    [headView addSubview:dateLabel];
    UIButton *tipButton = [UIButton buttonWithImage:@"grayTipDown" selectImage:@"grayTipUp"];
    tipButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    tipButton.selected = [self.closeArray containsObject:@(section)]?NO:YES;
    tipButton.tag = 1000+section;
    [tipButton addTarget:self action:@selector(openOrCloseData:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:tipButton];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).offset(viewPix(16));
        make.bottom.equalTo(headView).offset(-viewPix(8));
    }];
    [tipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(dateLabel);
        make.right.equalTo(dateLabel).offset(viewPix(20));
        make.height.equalTo(@(viewPix(30)));
    }];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return footView;
}


#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(40)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientReportCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}
-(LGPatientCTReportView *)headView{
    if(!_headView){
        _headView = [[LGPatientCTReportView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(310))];
    }
    return _headView;
}

-(NSMutableArray *)closeArray{
    if(!_closeArray){
        _closeArray = [NSMutableArray array];
    }
    return _closeArray;
}

@end
